from datetime import datetime

from django.db import models
from django.contrib.auth import get_user_model

from commando.models import Answer, Problem, Task, Tag

User = get_user_model()


class FollowProblem(models.Model):
    """
    关注问题
    """
    problem = models.ForeignKey(Problem, blank=False, null=False, verbose_name="关注的问题", on_delete=models.CASCADE,
                                help_text='关注的问题')
    user = models.ForeignKey(User, verbose_name='用户', on_delete=models.CASCADE, help_text='用户')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '关注问题'
        verbose_name_plural = verbose_name
        # unique_together = ('user', 'problem') # 这种方式是用数据库进行设置

    def __str__(self):
        return self.problem.title


class FollowUser(models.Model):
    """
    关注用户
    """
    followed_user = models.ForeignKey(User, blank=False, null=False, verbose_name="关注的用户", on_delete=models.CASCADE,
                                      related_name='followed', help_text='关注的用户')
    following_user = models.ForeignKey(User, blank=False, null=False, verbose_name='用户', on_delete=models.CASCADE,
                                       related_name='following', help_text='用户本身')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '关注用户'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.followed_user.username


class FavAnswer(models.Model):
    """
    收藏回答
    """
    answer = models.ForeignKey(Answer, blank=False, null=False, verbose_name="收藏的回答", on_delete=models.CASCADE,
                               help_text='收藏的回答')
    user = models.ForeignKey(User, blank=False, null=False, verbose_name='用户', on_delete=models.CASCADE, help_text='用户')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '收藏回答'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.username


class AgreeOppositeAnwser(models.Model):
    """
    对答案的赞同
    """
    agree_type = ((True, '已赞同'), (False, '未赞同'))
    opposite_type = ((True, '已反对'), (False, '未反对'))

    answer = models.ForeignKey(Answer, verbose_name="回答", on_delete=models.CASCADE, help_text='对应的答案')
    user = models.ForeignKey(User, verbose_name="用户", on_delete=models.CASCADE, help_text='用户')
    agree = models.BooleanField(choices=agree_type, default=False, blank=False, null=False, verbose_name="赞同",
                                help_text='赞同')
    opposite = models.BooleanField(choices=opposite_type, default=False, blank=False, null=False, verbose_name="反对",
                                   help_text='反对')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = "赞同答案"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.answer


class ShareUser(models.Model):
    """
    分享用户
    """
    share_user = models.ForeignKey(User, blank=False, null=False, verbose_name="分享的用户", on_delete=models.CASCADE,
                                   related_name='share_user', help_text='分享的用户')
    user = models.ForeignKey(User, blank=False, null=False, verbose_name='用户', on_delete=models.CASCADE, help_text='用户')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '分享用户'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.share_user.username


class ShareAnswer(models.Model):
    """
    分享答案
    """
    answer = models.ForeignKey(Answer, blank=False, null=False, verbose_name="分享的答案", on_delete=models.CASCADE,
                               help_text='分享的答案')
    user = models.ForeignKey(User, blank=False, null=False, verbose_name='用户', on_delete=models.CASCADE, help_text='用户')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '分享答案'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.answer


class ShareProblem(models.Model):
    """
    分享问题
    """
    problem = models.ForeignKey(Problem, blank=False, null=False, verbose_name="分享问题", on_delete=models.CASCADE,
                                help_text='分享的问题')
    user = models.ForeignKey(User, blank=False, null=False, verbose_name='用户', on_delete=models.CASCADE, help_text='用户')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '分享问题'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.problem.title


class Invitation(models.Model):
    """
    邀请回答问题
    """
    problem = models.ForeignKey(Problem, blank=True, null=True, on_delete=models.CASCADE, verbose_name="问题",
                                help_text='问题')
    invite_user = models.ForeignKey(User, blank=False, null=False, on_delete=models.CASCADE, verbose_name='邀请的用户',
                                    related_name='invite_user', help_text='邀请的用户')
    user = models.ForeignKey(User, blank=False, null=False, verbose_name="用户", on_delete=models.CASCADE, help_text='用户')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '邀请回答问题'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.username


class ProblemToTag(models.Model):
    """
    为问题增加标签
    """
    user = models.ForeignKey(User, blank=False, null=False, verbose_name='创建该问题用户', on_delete=models.CASCADE,
                             help_text='创建该问题用户')
    tag = models.ForeignKey(Tag, on_delete=models.CASCADE, verbose_name="标签", null=False, blank=False, help_text='标签')
    problem = models.ForeignKey(Problem, null=False, blank=False, on_delete=models.CASCADE, verbose_name='添加标签所对应的问题',
                                help_text='添加标签所对应的问题')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = "问题-标签"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.tag.name


